export default {
  // 为数字添加小数点
  addZero (val, len) {
    let decimalArr = []
    val = val.toString()
    let valArr = val.split('.')
    let _len = len || 2
    if (!val) {
      return ''
    }
    if (val.indexOf('.') === -1) {
      decimalArr.push('.')
    } else {
      _len = _len - valArr[1].length
    }
    if (_len > 0) {
      for (let i = 0; i < _len; i++) {
        decimalArr.push('0')
      }
      val = val + decimalArr.join('')
    } else if (_len < 0) {
      valArr[1] = valArr[1].substr(0, 2)
      val = valArr.join('.')
    }
    return val
  },
  // 加法运算
  numAdd (num1, num2) {
    let baseNum
    let baseNum1
    let baseNum2
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
    return (num1 * baseNum + num2 * baseNum) / baseNum
  },
  // 减法运算
  numSub (num1, num2) {
    let baseNum
    let baseNum1
    let baseNum2
    let precision // 精度
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
    precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2
    return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision)
  },
  // 乘法运算
  numMulti (num1, num2) {
    let baseNum = 0
    try {
      baseNum += num1.toString().split('.')[1].length
    } catch (e) {
    }
    try {
      baseNum += num2.toString().split('.')[1].length
    } catch (e) {
    }
    return Number(num1.toString().replace('.', '')) * Number(num2.toString().replace('.', '')) / Math.pow(10, baseNum)
  },
  // 除法运算
  numDiv (num1, num2) {
    let baseNum1 = 0
    let baseNum2 = 0
    let baseNum3
    let baseNum4
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }
    baseNum3 = Number(num1.toString().replace('.', ''))
    baseNum4 = Number(num2.toString().replace('.', ''))
    return (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1)
  },
  // 数字转中文大写
  numToCap (Num) {
    for (let i = Num.length - 1; i >= 0; i--) {
      Num = Num.replace(',', '') // 替换Num中的“,”
      Num = Num.replace(' ', '') // 替换Num中的空格
    }
    if (isNaN(Num)) { // 验证输入的字符是否为数字
      return
    }
    // 字符处理完毕后开始转换，采用前后两部分分别转换
    let part = String(Num).split('.')
    let newchar = ''
    // 小数点前进行转化
    for (let i = part[0].length - 1; i >= 0; i--) {
      if (part[0].length > 10) {
        // alert("位数过大，无法计算");
        return ''
      } // 若数量超过拾亿单位，提示
      let tmpnewchar = ''
      let perchar = part[0].charAt(i)
      switch (perchar) {
        case '0':
          tmpnewchar = '零' + tmpnewchar
          break
        case '1':
          tmpnewchar = '一' + tmpnewchar
          break
        case '2':
          tmpnewchar = '二' + tmpnewchar
          break
        case '3':
          tmpnewchar = '三' + tmpnewchar
          break
        case '4':
          tmpnewchar = '四' + tmpnewchar
          break
        case '5':
          tmpnewchar = '五' + tmpnewchar
          break
        case '6':
          tmpnewchar = '六' + tmpnewchar
          break
        case '7':
          tmpnewchar = '七' + tmpnewchar
          break
        case '8':
          tmpnewchar = '八' + tmpnewchar
          break
        case '9':
          tmpnewchar = '九' + tmpnewchar
          break
      }
      switch (part[0].length - i - 1) {
        case 0:
          // tmpnewchar = tmpnewchar
          break
        case 1:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '十'
          break
        case 2:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '百'
          break
        case 3:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '千'
          break
        case 4:
          tmpnewchar = tmpnewchar + '万'
          break
        case 5:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '十'
          break
        case 6:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '百'
          break
        case 7:
          if (perchar !== 0) tmpnewchar = tmpnewchar + '千'
          break
        case 8:
          tmpnewchar = tmpnewchar + '亿'
          break
        case 9:
          tmpnewchar = tmpnewchar + '十'
          break
      }
      newchar = tmpnewchar + newchar
    }
    // 替换所有无用汉字，直到没有此类无用的数字为止
    while (newchar.search('零零') !== -1 || newchar.search('零亿') !== -1 || newchar.search('亿万') !== -1 || newchar.search('零万') !== -1) {
      newchar = newchar.replace('零亿', '亿')
      newchar = newchar.replace('亿万', '亿')
      newchar = newchar.replace('零万', '万')
      newchar = newchar.replace('零零', '零')
    }
    // 替换以“一十”开头的，为“十”
    if (newchar.indexOf('一十') === 0) {
      newchar = newchar.substr(1)
    }
    // 替换以“零”结尾的，为“”
    if (newchar.lastIndexOf('零') === newchar.length - 1) {
      newchar = newchar.substr(0, newchar.length - 1)
    }
    return newchar
  }
}
